package com.interesting.client.UI.home;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.interesting.R;
import com.interesting.base.core.BaseActivity;
import com.interesting.base.http.HttpUtil;
import com.interesting.client.UI.commonActivity.UIUtil;
import com.interesting.client.UI.setting.LoginActivity;
import com.interesting.client.application.AppHolder;
import com.interesting.client.support.crop.Crop;

/** 投稿 界面 */
public class SendMesgActivity extends BaseActivity {
	String tag = "send";
	private Crop messageCrop;
	private ImageView send, photo;
	private EditText content;
	private TextView textsub;
	private boolean existPhoto = false; // 用于判断是否有图片

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getSupportActionBar().setCustomView(R.layout.text_actionbar_custom_view);
		setTitle(getResources().getString(R.string.send));
		setContentView(R.layout.home_send_message_activity);

		textsub = (TextView) findViewById(R.id.home_send_message_textsub);
		send = (ImageView) findViewById(R.id.home_send_message_submit);
		photo = (ImageView) findViewById(R.id.home_send_message_photo);
		content = (EditText) findViewById(R.id.home_send_message_content);

		messageCrop = new Crop(SendMesgActivity.this);
		messageCrop.setImageView(photo);
		OnClickListener listener = new OnClickListener() {
			@Override
			public void onClick(View v) {
				switch (v.getId()) {
				case R.id.home_send_message_textsub:
					;
				case R.id.home_send_message_submit: // 发送
					String userId = AppHolder.getInsatnce().user.getUserId();
					if (UIUtil.isLoggedIn()) {
						toSend(userId, content.getText().toString(), existPhoto);
					} else {
						UIUtil.showLongToast(getApplicationContext(), "请先登录后在投稿...");
						Intent toLogin = new Intent(SendMesgActivity.this, LoginActivity.class);
						startActivity(toLogin);
					}
					break;
				
				case R.id.home_send_message_photo: // 添加图片
					messageCrop.showDialog();
					break;
				}
			}
		};
		textsub.setOnClickListener(listener);
		send.setOnClickListener(listener);
		photo.setOnClickListener(listener);
	}

	private void toSend(String userId, String text, boolean existphoto) {
		if (text.length() < 5) { // 文字内容过少 进行提示
			UIUtil.showToast(getApplicationContext(), "莫要惜字如金啊");
		} else if (existphoto) { // 有图片
			Bitmap bitmap = ((BitmapDrawable) photo.getDrawable()).getBitmap();
			UIUtil.showWaitDialog(getSupportFragmentManager());
			new SendMesgAsyncTask().execute(userId, text, bitmap);
		} else {// 无图片
			UIUtil.showWaitDialog(getSupportFragmentManager());

			new SendMesgAsyncTask().execute(userId, text, null);
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
		if (resultCode == Activity.RESULT_OK) {
			switch (requestCode) {
			case 1:
				messageCrop.onActivityResult(requestCode, resultCode, intent);
				break;
			case 2:
				messageCrop.onActivityResult(requestCode, resultCode, intent);
				existPhoto = true;
				break;
			case 3:
				messageCrop.onActivityResult(requestCode, resultCode, intent);
				break;
			}
		}
		super.onActivityResult(requestCode, resultCode, intent);
	}

	/** 投稿 */
	class SendMesgAsyncTask extends AsyncTask<Object, Void, String> {

		@Override
		protected String doInBackground(Object... params) {
			Map<String, Object> request = new HashMap<String, Object>();
			request.put("act", "addInfo");
			request.put("userId", params[0]);
			request.put("content", params[1]);
			request.put("photo", params[2]);
			return HttpUtil.postImage(request);
		}

		@Override
		protected void onPostExecute(String result) {
			JSONObject response = HttpUtil.parseResult(result);
			UIUtil.hideDialog(getSupportFragmentManager());
			if (response != null) {
				UIUtil.showToast(getApplicationContext(), "投稿成功");
				finish();
			}
			super.onPostExecute(result);
		}

	}
}
